package my;

import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class DbInit implements CommandLineRunner {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void run(String... args) throws Exception {

        jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS"
                + " USERS (id INT PRIMARY KEY, name VARCHAR(255))");

        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM USERS where id=1");
        if (list.isEmpty()) {
            jdbcTemplate.execute("INSERT INTO USERS (id, name) VALUES (1, '张三')");
        }
    }
}
